﻿
function dealImage(base64, maxWidth, maxHeight, quality) {
    return new Promise((resolve, reject) => {
        const img = new Image();
        img.src = base64;
        img.onload = () => {
            const canvas = document.createElement('canvas');
            const ctx = canvas.getContext('2d');
            //const ratio = Math.min(maxWidth / img.width, maxHeight / img.height);
            //canvas.width = img.width * ratio;
            //canvas.height = img.height * ratio;
            canvas.width = img.width;
            canvas.height = img.height;
            ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
            canvas.toBlob((blob) => {
                const compressedBase64 = URL.createObjectURL(blob);
                resolve(compressedBase64);
            }, 'image/jpeg', quality);
        };
        img.onerror = (error) => {
            reject(error);
        };
    });
}